#include "threadsafe_queue.hpp"

#include <thread>
#include <iostream>
#include <chrono>

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define _random(x) (rand()%x)

using namespace std;

static threadsafe_queue<int> g_queue;
std::mutex g_mutex;

int random(int num)
{
	srand((int)time(0));
	return _random(num);
}

void fun_inputdata()
{
	while (1)
	{
		srand((int)time(0));
		int num = random(100);
		g_queue.push(num);

		g_mutex.lock();
		std::cout << "[" << this_thread::get_id() << "]inputdata: " << num << std::endl;
		g_mutex.unlock();

		this_thread::sleep_for(chrono::seconds(1));
	}
}

void fun_getdata()
{
	while (1)
	{
		int num = 0;
		g_queue.wait_and_pop(num);

		g_mutex.lock();
		std::cout << "["<<this_thread::get_id()<<"] dataget:" << num << std::endl;
		g_mutex.unlock();

		this_thread::sleep_for(chrono::milliseconds(random(1000)));
	}
}

int main()
{
	std::thread thread_input(fun_inputdata);
	std::thread thread_get(fun_getdata);
	std::thread thread_get1(fun_getdata);
	std::thread thread_get2(fun_getdata);
	std::thread thread_get3(fun_getdata);

	thread_input.join();
	thread_get.join();
	thread_get1.join();
	thread_get2.join();
	thread_get3.join();

	//g_queue.push(10);

	getchar();
    return 0;
}

